package com.leetcode.LC;

public class LC29 {
    public static void main(String[] args) {
        System.out.println(Integer.MAX_VALUE + "\n" + Integer.MIN_VALUE);
        int divide = new LC29().divide(-2147483648, 2);
        System.out.println(divide);

    }

    public int divide(int dividend, int divisor) {
        if (dividend == Integer.MIN_VALUE && divisor == 1) {
            return Integer.MIN_VALUE;
        }
        if (dividend == Integer.MIN_VALUE && divisor == -1) {
            return Integer.MAX_VALUE;
        }
        long y = Math.abs(dividend);
        long x = Math.abs(divisor);
        long n = -1;


        if (dividend == 0) {
            return 0;
        }
        if ((dividend > 0 && divisor > 0) && (dividend < divisor)) {
            return 0;
        }
        if ((dividend < 0 && divisor < 0) && (dividend > divisor)) {
            return 0;
        }

/*        if (divisor==0){
            return 0;
        }*/


        long t1 = 0;
        long t2 = 0;

        while (true) {
            if (x == 1) {
                n = y;
                break;
            }
            if (x == y) {
                n = 1;
                break;
            }
            t2 = t1 + x;
            n = n + 1;
            if (t2 == y) {
                n = n + 1;
                break;
            }
            if ((t1 < y && t2 > y)) {
                break;
            }
            t1 = t2;
        }
        if ((dividend < 0 && divisor < 0)) {
            if ((n > Integer.MAX_VALUE)) {
                return Integer.MAX_VALUE;
            }
            return (int) n;
        }

        if ((dividend > 0 && divisor > 0)) {//判断符号
            if ((n > Integer.MAX_VALUE)) {
                return Integer.MAX_VALUE;
            }
            return (int) n;
        } else {
            if ((-n < Integer.MIN_VALUE)) {
                return Integer.MAX_VALUE;
            }
            return (int) -n;
        }
    }
}
